package com.film.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.film.po.User;
import com.film.service.UserService;
import com.film.token.JwtUtil;

@RestController
public class UserController{

	private UserService userService;
	
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public JwtUtil jwtUtil;

	@Autowired
	public void setJwtUtil(JwtUtil jwtUtil) {
		this.jwtUtil = jwtUtil;
	}

	@RequestMapping(value = "/user", method = RequestMethod.POST)
	public ResponseEntity<User> add(@RequestBody User user) {
		try {
			User user2 = userService.add(user);
			user2.setPassword(null);
			return new ResponseEntity<User>(user2, HttpStatus.CREATED);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<User>(HttpStatus.CONFLICT);
		
	}

	@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<Void> deleteById(@PathVariable("id") int id) {
		try {
			userService.deleteById(id);
			return new ResponseEntity<Void>( HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
		
		
	}
	
	@RequestMapping(value = "/user/status", method = RequestMethod.POST)
	public ResponseEntity<String> login(@RequestBody User user) {
		try {
			User user2 = userService.findByUsername(user.getUsername());
			if(user2!=null && user2.getPassword().equals(user.getPassword())) {
				String token = jwtUtil.createJWT(user2.getUserId()+"", JwtUtil.generalSubject(user2.getUserId()+""), 1000L*60*60);
				return new ResponseEntity<String>(token, HttpStatus.OK);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
	}

	@RequestMapping(value = "/user", method = RequestMethod.PUT)
	public ResponseEntity<User> update(@RequestBody User user) {
		try {
			User user2 = userService.update(user);
			user2.setPassword(null);
			return new ResponseEntity<User>(user2, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
		
	}

	@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
	public ResponseEntity<User> findById(@PathVariable("id") int id) {
		try {
			User user = userService.findById(id);
			user.setPassword(null);
			return new ResponseEntity<User>(user, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
		
	}
	
	

	@RequestMapping(value = "/users", method = RequestMethod.GET)
	public ResponseEntity<List<User>> getList() {
		try {
			List<User> list = userService.getList();
			for (User user : list) {
				user.setPassword(null);
			}
			return new ResponseEntity<List<User>>(list, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
		
	}

}
